/*
 * Package il.ac.biu.cs.grossmm.api.presence
 * File Winfo.java
 * 
 * This is a part of presence service framework API. 
 * See javadoc for more information.
 *  
 * Copyright (C) 2006 Grossmann Mark
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package il.ac.biu.cs.grossmm.api.presence;

import java.util.WeakHashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Represents watcher information resource type. Watcher information is a meta
 * resource. It always refers to a another resource.
 */
public class Winfo implements ResourceType {
	private final ResourceType about;

	private static final WeakHashMap<ResourceType, Winfo> instances =
		new WeakHashMap<ResourceType, Winfo>();

	private static final Lock lock = new ReentrantLock();

	/**
	 * Gets a watcher information resource type for a given resource type
	 * @param resourceType resource type subscribtions to which are described
	 * by the watcher information resource type
	 * @return
	 */
	public static Winfo winfo(ResourceType resourceType)
	{
		Winfo winfo;

		lock.lock();

		try {
			winfo = instances.get(resourceType);

			if (winfo == null) {
				winfo = new Winfo(resourceType);
				instances.put(resourceType, winfo);
			}
		} finally {
			lock.unlock();
		}

		return winfo;
	}

	private Winfo(ResourceType resourceType)
	{
		this.about = resourceType;
	}

	ResourceType about()
	{
		return about;
	}

	@Override
	public boolean equals(Object obj)
	{
		if (obj == this)
			return true;

		if (!(obj instanceof Winfo))
			return false;

		Winfo w = (Winfo) obj;

		return w.about.equals(about);
	}

	@Override
	public int hashCode()
	{
		return 0xc001f001 ^ about.hashCode();
	}
	
	@Override
	public String toString()
	{
		return "WINFO("+about+")";
	}
}
